<template>
  <div ref="target" class="home-product">
    <HomePanel v-for="item in list" :key="item.id" :title="item.name">
      <template #right>
        <div class="sub">
          <router-link v-for="subTitle in item.children"
                       :key="subTitle.id"
                       :to="`/category/sub/${subTitle.id}`">
            {{ subTitle.name }}
          </router-link>
        </div>
        <XtxMore :path="`/category/${item.id}`"></XtxMore>
      </template>

      <template #default>
        <div class="box">
          <router-link :to="`/category/${item.id}`" class="cover">
            <img v-lazy="item.picture" alt="">
            <strong class="label">
              <span>{{ item.name }}馆</span>
              <span>{{ item.saleInfo }}</span>
            </strong>
          </router-link>
          <ul class="goods-list">
            <li v-for="goods in item.goods" :key="goods.id">
              <HomeGoods :goods="goods"></HomeGoods>
            </li>
          </ul>
        </div>
      </template>
    </HomePanel>
  </div>
</template>

<script>
import HomePanel from '@/components/home/HomePanel'
import XtxMore from '@/components/library/XtxMore'
import HomeGoods from '@/components/home/HomeGoods'

import API from '@/api/home'
import { useLazyData } from '@/hooks'

export default {
  name: 'HomeProduct',
  components: {
    HomeGoods,
    XtxMore,
    HomePanel
  },
  setup() {
    const {
      result: list,
      target
    } = useLazyData(API.getProduct)

    return {
      list,
      target
    }
  }
}
</script>

<style lang="less" scoped>

.home-product {
  width: 100%;
  height: 2900px;
  background-color: #fff;

  .sub {
    text-align: center;
    margin-bottom: 2px;

    a {
      padding: 2px 12px;
      font-size: 16px;

      &:hover {
        background-color: @xtxColor;
        border-radius: 4px;
        color: #fff;
      }

      &:last-child {
        margin-right: 80px;
      }
    }
  }

  .box {
    display: flex;

    .cover {
      width: 240px;
      height: 610px;
      margin-right: 10px;
      position: relative;

      img {
        width: 100%;
        height: 100%;
        // 对图片进行剪切, 保留原始比例
        // object-fit: cover, 保持原有尺寸比例, 但是内容可能被剪切
        object-fit: cover;
      }

      .label {
        display: flex;
        position: absolute;
        left: 0;
        top: 50%;
        transform: translateY(-50%);

        width: 188px;
        height: 66px;

        font-size: 18px;
        color: #fff;
        line-height: 66px;
        font-weight: normal;

        span {
          text-align: center;

          &:first-child {
            width: 76px;
            background: rgba(0, 0, 0, 0.9);
          }

          &:last-child {
            flex: 1;
            background: rgba(0, 0, 0, 0.7);
          }
        }
      }
    }

    .goods-list {
      width: 990px;
      display: flex;
      flex-wrap: wrap;

      li {
        width: 240px;
        height: 300px;
        margin-right: 10px;
        margin-bottom: 10px;

        // 匹配父元素中倒数第 -n + 4 个子元素, 并且是li元素
        &:nth-last-child(-n+4) {
          margin-bottom: 0;
        }

        // 每一行的最后一个元素, 没有右外边距
        &:nth-child(4n) {
          margin-right: 0;
        }
      }
    }
  }
}
</style>
